package com.autobet.services.thread;

import com.autobet.services.BetService;
import com.autobet.services.EnvConfigService;
import com.autobet.services.ResultService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by yangd on 2017-08-27.
 */
public class BetThread extends Thread {
    private static final Logger log = LoggerFactory.getLogger(BetThread.class);
    private long lSleepTime = 30000L;
    private String name;
    private String betType;

    public BetThread(String name, String betType) {
        this.name = name;
        this.betType = betType;
    }

    @Override
    public void run() {
        ResultService resultService = (ResultService)EnvConfigService.getApplicationContext().getBean(ResultService.class);
        BetService betService = (BetService)EnvConfigService.getApplicationContext().getBean(BetService.class);

        while("RUNING".equals(EnvConfigService.getTaskStatus())) {
            try {
                Thread.sleep(this.lSleepTime);
            } catch (InterruptedException var5) {
                betService.stopBetForException("[系统预警]下单线程异常，请暂停系统，重新执行下单!");
                return;
            }

            try {
                betService.doBet(this.betType);
            } catch (Exception var4) {
                log.info("下单失败，停止任务");
                betService.stopBetForException("下单失败，停止任务");
                return;
            }
        }

        log.info("系统执行状态不正确，退出[下单任务]");
        resultService.addInfo("系统执行状态不正确，退出[下单任务],请重新激活..", "01");
    }
}

